﻿#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable : 6031)
#include "stack.h"

void stinit(st* pst)
{
	assert(pst);

	pst->p = NULL;
	pst->top = 0;		//这里top也可以赋值-1
	pst->num = 0;
}

void stpush(st* pst, DataType x)
{
	assert(pst);
	if (pst->top == pst->num)
	{
		int newnum = pst->top == 0 ? 4 : pst->num * 2;
		DataType* newp = (DataType*)realloc(pst->p, sizeof(DataType) * newnum);
		if (!newp)
		{
			perror("realloc");
			return;
		}
		pst->p = newp;
		pst->num = newnum;
	}
	pst->p[pst->top] = x;
	pst->top++;
}

void stpop(st* pst)
{
	assert(pst);
	assert(!stempty(pst));

	pst->top--;
}

void stdestroy(st* pst)
{
	assert(pst);

	free(pst->p);
	pst->p == NULL;
	pst->num = 0;
	pst->top = 0;
}

bool stempty(st* pst)
{
	assert(pst);

	/*if (!pst->top)
	{
		return true;
	}
	else
	{
		return false;
	}*/

	return pst->top == 0;
}

DataType sttop(st* pst)
{
	assert(pst);

	return pst->p[pst->top - 1];
}

DataType stsize(st* pst)
{
	assert(pst);
	return pst->top;
}